package com.tuanzi.loan.business.repository.family.impl;

import com.tuanzi.loan.business.repository.family.custom.LoanChannelNdInfoRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.family.LoanChannelNdInfoRequest;
import com.tuanzi.loan.vo.response.family.LoanChannelNdInfoResponse;
import org.jooq.Record1;
import org.jooq.SelectJoinStep;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import java.util.List;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanChannelNdInfo.LOAN_CHANNEL_ND_INFO;

/**
 * User: liangZongXi
 * Create time: 15:47 2019/8/6
 * Description:
 */
@Repository
public class LoanChannelNdInfoRepositoryImpl extends JOOQBaseRepository implements LoanChannelNdInfoRepositoryCustom {

    @Override
    public Page<LoanChannelNdInfoResponse> list(LoanChannelNdInfoRequest searchRequest, Pageable pageable) {
        fields();
        contains(LOAN_CHANNEL_ND_INFO.CHANNEL_ND, searchRequest.getLargeChannel());
        contains(LOAN_CHANNEL_ND_INFO.CHANNEL_ND_NAME, searchRequest.getLargeChannelName());
        contains(LOAN_CHANNEL_ND_INFO.CHANNEL_INFO, searchRequest.getChannel());
        orderBy(LOAN_CHANNEL_ND_INFO.ID, Sort.Direction.DESC);
        return search(pageable, record -> {
            LoanChannelNdInfoResponse response = new LoanChannelNdInfoResponse();
            response.setId(record.get(LOAN_CHANNEL_ND_INFO.ID));
            response.setChannelInfo(record.get(LOAN_CHANNEL_ND_INFO.CHANNEL_INFO));
            response.setLargeChannel(record.get(LOAN_CHANNEL_ND_INFO.CHANNEL_ND));
            response.setLargeChannelName(record.get(LOAN_CHANNEL_ND_INFO.CHANNEL_ND_NAME));
            return response;
        });
    }

    @Override
    public List<String> findDistinctChannelNd() {
        SelectJoinStep<Record1<String>> from = dslContext.selectDistinct(LOAN_CHANNEL_ND_INFO.CHANNEL_ND)
                .from(LOAN_CHANNEL_ND_INFO);
        return from.fetchInto(String.class);
    }

    @Override
    protected TableImpl getTable() {
        return LOAN_CHANNEL_ND_INFO;
    }
}
